﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using STX.Domain;

namespace STX.Admin.Models.BizActivity
{
    public class EditCostModel : BaseAddModel
    {
        public EditCostModel()
        {
            TemplateQuotePrices = new List<Domain.BizQuotePriceTemplate>();
        }
        public IList<STX.Domain.BizQuotePriceTemplate> TemplateQuotePrices { get; set; }
        public IList<STX.Domain.BizMaterialCategory> MaterialCategories { get; set; }

        public int Id { get; set; }
        public int AreaId { get; set; }
        public int ProjectId { get; set; }
        public string Name { get; set; }
        public string ProjectName { get; set; }
        
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }
        public string TimeDetail { get; set; }
        public string Place { get; set; }
        public string ResponsiblePerson { get; set; }
        public string QuotationTabulator { get; set; }
        public DateTime QuotationGeneratedTime { get; set; }
        public string Executor { get; set; }
        public string Planner { get; set; }
        public string Driver { get; set; }
        public UserBehave UserBehave { get; set; }
        public StepEnum Step { get; set; }
        public string Comment { get; set; }

        public List<BizActivityItem> BizActivityItems { get; set; }
        public IQueryable<STX.Domain.BizActivityItemSupplier> BizActivityItemSuppliers { get; set; }

        public IList<STX.Domain.BizSupplier> GetSuppliersByTpCode(string tpCode)
        {
            if (tpCode.IsNullOrEmpty()) return null;
            var quotePriceTemplate = TemplateQuotePrices.FirstOrDefault(o => o.Code.Trim() == tpCode.Trim());
            return quotePriceTemplate != null ? quotePriceTemplate.Suppliers : null;
        }

        public bool IsNeedSplit(string tpCode)
        {
            if (tpCode.IsNullOrEmpty()) return false;
            var quotePriceTemplate = TemplateQuotePrices.FirstOrDefault(o => o.Code.Trim() == tpCode.Trim());
            return quotePriceTemplate != null && quotePriceTemplate.Split== Domain.BizQuotePriceTemplate.SplitEnum.NeedSplit;
        }

        public bool CheckSupplierIsSelected(int activityItemId,int supplierId)
        {
            return BizActivityItemSuppliers != null 
                && BizActivityItemSuppliers.Any(o => o.ActivityItemId == activityItemId && o.SupplierId == supplierId);
        }

        public bool CheckActivityHasSupplier(int activityItemId)
        {
            return BizActivityItemSuppliers != null 
                && BizActivityItemSuppliers.Any(o => o.ActivityItemId == activityItemId);
        }
    }
}